package com.zhitong.money.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhitong.money.entity.Leave;
import com.zhitong.money.exception.AddException;
import com.zhitong.money.exception.DeleteException;
import com.zhitong.money.exception.UpdateException;
import com.zhitong.money.mapper.LeaveMapper;
import com.zhitong.money.services.api.ILeaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class LeaveService implements ILeaveService {

    @Autowired
    private LeaveMapper mapper;


    @Override
    public List<Leave> findAllLeave() throws Exception {

        List<Leave> allLeave = mapper.findAllLeave();
        if (allLeave==null || allLeave.isEmpty()){
            throw new NullPointerException("findAllLeave:查询为空");
        }

        return allLeave;

    }

    @Override
    public PageInfo findLeaveByPage(Map<String,Object> map) throws Exception {
        int pageNum=map.containsKey("pageNum")?(int)map.get("pageNum"):1;
        int pageSize=map.containsKey("pageSize")?(int)map.get("pageSize"):5;

        PageHelper.startPage(pageNum,pageSize);
        List<Leave> allLeave = mapper.findLeaveByMap(map);
        if (allLeave==null || allLeave.isEmpty()){
            throw new NullPointerException("findLeaveByPage:查询为空");
        }

        PageInfo pageInfo=new PageInfo(allLeave);
        return pageInfo;
    }

    @Override
    public List<Leave> findLeaveByMap(Map<String, Object> map) throws Exception {
        List<Leave> leaveByMap = mapper.findLeaveByMap(map);
        if (leaveByMap==null || leaveByMap.isEmpty()){
            throw new NullPointerException("findLeaveMap:查询为空");
        }
        return leaveByMap;
    }

    @Override
    public List<Leave> findLeaveByTid(String tid) throws Exception {
        List<Leave> leaves = mapper.findLeaveByTid(tid);
        if (leaves==null){
            throw new NullPointerException("findLeaveByTid:查询为空");
        }

        return leaves;
    }

    @Override
    public Leave findLeaveById(String id) throws Exception {

        Leave leave = mapper.findLeaveById(id);
        if (leave==null){
            throw new NullPointerException("findLeaveById:查询为空");
        }

        return leave;

    }

    @Override
    public void addLeave(Leave leave) throws Exception {
        try {
            //查询是否还有邮箱的假条(有的话,就添加失败)
            List<Leave> leaveByNew = mapper.findLeaveByNew(leave.getTid());
            if (leaveByNew!=null && !leaveByNew.isEmpty()){
                throw new AddException("addLeave:您的假期还未失效");
            }

            mapper.addLeave(leave);
        } catch (Exception e) {
            throw new AddException("addLeave:添加错误!"+e.getMessage());
        }
    }

    @Override
    public void updateLeave(Leave leave) throws Exception {
        try {


            mapper.updateLeave(leave);
        } catch (Exception e) {
            throw new UpdateException("updateLeave:更新失败!"+e.getMessage());
        }
    }

    @Override
    public void updateSelfEnable(String id ,String enable) throws Exception {
        try {
            mapper.updateSelfEnable(id,enable);
        } catch (Exception e) {
            throw new UpdateException("updateSelfEnable:更新失败!"+e.getMessage());
        }

    }

    @Override
    public void updateEnable(String id ,String enable) throws Exception {
        try {
            mapper.updateEnable(id,enable);
        } catch (Exception e) {
            throw new UpdateException("updateEnable:更新失败!"+e.getMessage());
        }

    }

    @Override
    public void delLeave(String id) throws Exception {
        try {
            mapper.delLeave(id);
        } catch (Exception e) {
            throw new DeleteException("delLeave:删除失败"+e.getMessage());
        }
    }

    @Override
    public void delLeavesByIds(List<String> ids) throws Exception {
        try {
            mapper.delLeaveByIds(ids);
        } catch (Exception e) {
            throw new DeleteException("delLeavesByIds:删除失败"+e.getMessage());
        }
    }
}
